package com.miracle.main.module.mvp.presenter;

import android.app.Activity;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.miracle.basemvp.base.BasePresenter;
import com.miracle.basemvp.base.BaseResponse;
import com.miracle.basemvp.base.IPresenter;
import com.miracle.basemvp.lifecycle.rx.RxDefaultObserver;
import com.miracle.common.bean.User.User;
import com.miracle.common.state.user.LoginContext;
import com.miracle.common.state.user.LoginState;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.common.util.S;
import com.miracle.main.module.mvp.contract.FileContract;
import com.miracle.main.util.FileUploadObserver;
import com.miracle.main.util.MultipartBuilder;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;

/**
 * com.miracle.main.module.mvp.presenter
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/10 15:20
 * @see com.miracle.main.module.mvp.presenter
 */
public class FilePresenter extends BasePresenter<FileContract.View, FileContract.Model>
        implements IPresenter {

    @Inject
    public FilePresenter(FileContract.View view, FileContract.Model model) {
        super(view, model);
    }

    public void uploadFiles(LifecycleProvider<ActivityEvent> lifecycleProvider,List<String> list) {
        String url = "http://111.231.226.202:8091/wecloud/upload";
//        String url = "http://wecloud-china.cn:38578/wecloud/upload";
        List<File> files = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));
            files.add(file);
        }

        FileUploadObserver fileUploadObserver = new FileUploadObserver<ResponseBody>() {
            @Override
            public void onUploadSuccess(ResponseBody o) {
                Toast.makeText((Activity)mView, "onUploadSuccess", Toast
                        .LENGTH_SHORT).show();
                Log.d("WJQ", "onUploadSuccess");
            }

            @Override
            public void onUploadFail(Throwable e) {
                Log.d("WJQ", "onUploadFail");
                Log.d("WJQ", "onUploadFail: " + e);
            }

            @Override
            public void onProgress(int progress) {
                Log.d("WJQ", "onProgress");
            }
        };
        String session = PreferencesUtils.getPreferences(((Activity) mView), PreferencesUtils
                .State.USER_SESSIONID);
//        session = "4613EA6A50604AE9BD6F62C3E64413B6";
        MultipartBody body = MultipartBuilder.filesToMultipartBody(files,
                fileUploadObserver,session);

        mModel.uploadFiles(url, body, session)
                .compose(lifecycleProvider.<ResponseBody>bindUntilEvent
                        (ActivityEvent.DESTROY))
                .subscribe(fileUploadObserver);
    }
}
